<?php
namespace app\wechat\controller;
use EasyWeChat\Factory;
use think\Controller;
use app\Wxback;
use function GuzzleHttp\json_encode;

class Index extends Controller
{
        private  $app;
        public function checkSignature()
        {
           /* header("Content-Type:text/plain;charset=utf-8");
            $sign = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
            $token = "zaccure";
            $echostr = $_GET["echostr"];
            $tmpArr = array($token, $timestamp, $nonce);

            sort($tmpArr, SORT_STRING);
            $tmpStr = implode($tmpArr);
            $signature = sha1($tmpStr);

// 如果生成的签名和微信传过来的签名相同,
// 就把微信传过来的echostr参数传给微信服务器, 标明认证通过

            if ($signature == $sign) {
                ob_clean();
                header("Content-type: text/html; charset=utf-8");
                echo $echostr;
                exit;
            } else {
                echo false;
            }*/
            //   下面是第二种方法
            $config = [
                'app_id' => 'wxd1f2052225643e80',
                'secret' => 'ea78100703acc8d54bc3dd878abc3b13',
                'token' => 'zaccure',
                'response_type' => 'array',
                //...
            ];

            $this->app = Factory::officialAccount($config);

            $this->app->server->push(function ($message) {
                switch ($message['MsgType']) {
                    case 'event':
                        f($message['Event']== "subscribe")
                        return '欢迎关注';
                        break;
                    case 'text':
                        if($message['Content']== "你好")
                        return '你也好';
                        return '你是谁呀';
                        break;
                    case 'image':
                        return '收到图片消息';
                        break;
                    case 'voice':
                        return '收到语音消息';
                        break;
                    case 'video':
                        return '收到视频消息';
                        break;
                    case 'location':
                        return '收到坐标消息';
                        break;
                    case 'link':
                        return '收到链接消息';
                        break;
                    case 'file':
                        return '收到文件消息';
                    // ... 其它消息
                    default:
                        return '收到其它消息';
                        break;
                }
            });

            $response = $this->app->server->serve();
            $response->send();exit;
        }


    public function Wx()
    {
        $options = [
            'app_id'    => 'wxd1f2052225643e80',
            'secret'    => 'ea78100703acc8d54bc3dd878abc3b13',
            'token'     => 'easywechat',
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log',
            ],
            // ...
        ];

        $app = Factory::officialAccount($options);

        $server = $app->server;
        $user = $app->user;

        $server->push(function($message) use ($user) {
            $fromUser = $user->get($message['FromUserName']);

            return "{$fromUser->nickname} 您好！欢迎关注 1!";
        });

        $server->serve()->send();

    }
}
